﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenCvSharp
{
    /// <summary>
    /// 像素点结构体
    /// </summary>
    public struct PixelPoint
    {
        public int Index;
        public int X;
        public int Y;
        public float NormalizedX;
        public float NormalizedY;
    }

    class LightPixelManager
    {
        // 私有静态实例
        private static LightPixelManager _instance;

        // 300 归一化尺寸 33.55秒
        // 500 归一化 34.80秒
        public int normalizationSize = 500;

        // 灯具回路信息
        public byte[] chpixels = new byte[] {
                144, 134,
                159, 59,
                144, 134,
                159, 59,
                144, 134,
                159, 59,
                144, 134,
                159, 59,
                145, 145,
                145, 145,
                145, 116,
                145, 145,
                145, 145,
                145, 116,
                145, 145,
                145, 145,
                145, 116,
                145, 145,
                145, 145,
                145, 116,
                135, 135
            };


        //存储像素点的列表
        public List<PixelPoint> pixelPoints = new List<PixelPoint>();


        // 公共静态属性获取单例实例
        public static LightPixelManager shareMe()
        {
            if (_instance == null)
            {
                _instance = new LightPixelManager();
            }
            return _instance;
        }



        ///*******************************************************************************
        /// <summary>
        /// 从CSV文件中加载像素数据
        /// </summary>
        /// <param name="fileName"></param>
        ///*******************************************************************************
        public void LoadPixelDataFromCSV(string fileName)
        {
            try
            {
                // 构建文件路径
                string filePath = Path.Combine(Application.StartupPath, "pixdata", fileName + ".csv");
                if (!File.Exists(filePath))
                {
                    MessageBox.Show("文件不存在: " + filePath);
                    return;
                }

                // 清空现有的像素数据
                pixelPoints.Clear();

                // 读取所有行
                string[] lines = File.ReadAllLines(filePath);

                // 处理每一行数据
                foreach (string line in lines)
                {
                    // 跳过空行
                    if (string.IsNullOrWhiteSpace(line)) continue;

                    // 分割数据
                    string[] parts = line.Split(',');
                    if (parts.Length >= 3)
                    {
                        try
                        {
                            // 解析数据
                            int pixelIndex = int.Parse(parts[0]);
                            float xNormalized = float.Parse(parts[1]);
                            float yNormalized = float.Parse(parts[2]);
                            // parts[3] 和 parts[4] 暂时忽略，根据需要可以使用

                            // 转换为实际坐标
                            float xPosition = xNormalized * normalizationSize;
                            float yPosition = (1 - yNormalized) * normalizationSize; // 翻转Y轴坐标

                            // 创建像素点
                            PixelPoint pixel = new PixelPoint
                            {
                                Index = pixelIndex,
                                X = (int)xPosition,
                                Y = (int)yPosition,
                                NormalizedX = xNormalized,
                                NormalizedY = yNormalized
                            };

                            pixelPoints.Add(pixel);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取CSV文件时出错: " + ex.Message);
            }
        }




    }
}
